package com.back.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.back.business.ActivityTableServiceImpl;
import com.back.business.IActivityTableService;
import com.base.web.BaseAction;
import com.domain.ActivityTable;
import com.util.comm.Contants;
import com.util.comm.RequestUtil;

public class ActivityAction extends BaseAction {
	private ActivityTable activityTable;
	private IActivityTableService service;
	private Logger LOGGER = Logger.getLogger(ActivityAction.class);
	private int total;
	@SuppressWarnings("unchecked")
	private List rows;

	public ActivityAction() {
		service = new ActivityTableServiceImpl();
	}

	public String addOrUpdateActivty() {
		boolean isSave = false;
		try {
			if (activityTable == null) {
				activityTable = new ActivityTable();
			}
			if (null == activityTable.getId() || 0 == activityTable.getId()) {
				activityTable.setCreatTime(new Date());// 设置 创建时间 修改时，时间不改变
			}
			isSave = service.saveOrUpdateActi(activityTable);
		} catch (Exception e) {
			LOGGER.error("ActivityAction  save or update"
					+ activityTable.getType() + " error ", e);
		}
		if (isSave) {
			return "success";
		}
		return "error";

	}

	public String findActivityById() {
		String id = RequestUtil.getParam(request, "id", "");
		ActivityTable actObj = service.findActivityById(id);
		request.setAttribute("activityTable", actObj);
		if ("2".equals(actObj.getType())) {
			return "new";
		}
		if ("3".equals(actObj.getType())) {
			return "project";
		}
		return "";
	}

	public String searchActivityByType() {
		String type = RequestUtil.getParam(request, "type", "");
		String page = RequestUtil.getParam(request, "page", "");
		String row = RequestUtil.getParam(request, "rows", "");
		Map<String, String> parameMap = new HashMap<String, String>();
		parameMap.put("type", type);
		parameMap.put("page", page);
		parameMap.put("row", row);
		List<ActivityTable> aclist = service.findByMap(parameMap);
		int count = service.count(parameMap);
		if (aclist != null) {
			total = count;
			rows = aclist;
		}
		return Contants.JSONSTRING_RESULT_NAME;

	}

	public String deleteIdsAndType() {
		PrintWriter out = null;
		try {
			out = response.getWriter();
			String idStr = RequestUtil.getParam(request, "ids", "");
			String type = RequestUtil.getParam(request, "type", "");
			boolean isboolean = service.deleteByIdAndType(idStr, type);
			out.println("{result:" + isboolean + "}");
		} catch (IOException e) {
			e.printStackTrace();
			LOGGER.error("ActivityAction  deleteIdsAndType  error ", e);
		}

		return null;
	}

	public String findActivityByType() {
		String type = RequestUtil.getParam(request, "type", "");
		String lanType = RequestUtil.getParam(request, "lanType", "");
		Map<String,String> parameMap = new HashMap<String, String>();
		parameMap.put("type", type);
		parameMap.put("lanType", lanType);
		ActivityTable actObj = service.findActivityByType(parameMap);
		request.setAttribute("activityTable", actObj);
		if ("1".equals(type)) {
			if("1".equals(lanType)){
			return "introduce";
			}else{
				return "enintroduce";
			}
		}
		if ("4".equals(type)) {
			return "call";
		}
		return "";
	}

	public ActivityTable getActivityTable() {
		return activityTable;
	}

	public void setActivityTable(ActivityTable activityTable) {
		this.activityTable = activityTable;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	@SuppressWarnings("unchecked")
	public List getRows() {
		return rows;
	}

	@SuppressWarnings("unchecked")
	public void setRows(List rows) {
		this.rows = rows;
	}
}
